﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Data;
using Ultima.Xna.Input;

namespace Ultima.Xna.Graphics.UI
{
    public class GumpImage : UINode, IMouseAttachable, IGumpImage
    {
        private Texture2D _texture;
        private int _hue;
        private int _gumpId;
        private bool _dirty;
        private Color[] _textureData;

        public int Hue
        {
            get { return _hue; }
            set
            {
                _hue = value;
                _dirty = true;
            }
        }

        public int GumpId
        {
            get { return _gumpId; }
            set
            {
                _gumpId = value;
                _dirty = true;
            }
        }

        public Texture2D Texture
        {
            get { return _texture; }
            set
            {
                _texture = value;
            }
        }

        public virtual bool Moveable
        {
            get
            {
                bool moveable = false;

                if (_parent != null && _parent is IMouseAttachable)
                {
                    moveable = ((IMouseAttachable)_parent).Moveable;
                }

                return moveable;
            }
        }

        public override Vector2 Size
        {
            get
            {
                if (_texture != null)
                {
                    return new Vector2(_texture.Width, _texture.Height);
                }
                else
                {
                    return base.Size;
                }
            }
            set { base.Size = value; }
        }

        public GumpImage(Game game, IUIContainerNode parent)
            : base(game, parent)
        {

        }

        public override void Initialize(System.Xml.XmlElement element)
        {
            base.Initialize(element);

            _texture = Data.Gumps.CreateTexture(_graphicsDevice, _gumpId);
            _textureData = new Color[_texture.Width * _texture.Height];

            _texture.GetData<Color>(_textureData);
        }

        public override void HandleClick(MouseButtons button, bool down)
        {
            if (_defaultBehaviour)
            {
                if (down)
                {
                    _manager.AttachMouse(this);
                }
                else
                {
                    _manager.DettachMouse();
                }
            }
            else
            {
                base.HandleClick(button, down);
            }
        }
        
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.End();

            if (_spriteBatch.Effect != null)
            {
                _spriteBatch.Effect.Parameters["HueTexture"].SetValue(Hues.Texture);
                _spriteBatch.Effect.Parameters["Hue"].SetValue(new Vector2(_hue - 1, 0));
            }

            _spriteBatch.BeginLast();

            RectangleF renderBounds;
            CalculateRenderBounds(out renderBounds);

            _spriteBatch.Draw(_texture, renderBounds, Color.White);
            
            if (_spriteBatch.Effect != null)
            {
                _spriteBatch.Effect.Parameters["Hue"].SetValue(new Vector2(-1, 0));
            }

            _spriteBatch.End();
            _spriteBatch.BeginLast();

            base.Draw(gameTime);
        }

        public override void HitTest(ref float x, ref float y, out bool value)
        {
            base.HitTest(ref x, ref y, out value);

            if (value)
            {
                RectangleF renderBounds;
                CalculateRenderBounds(out renderBounds);

                float cx = x - renderBounds.X;
                float cy = y - renderBounds.Y;

                value = _textureData[(int)cx + ((int)cy * _texture.Width)].A > 0;
            }
        }
    }
}
